package com.pay.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.pay.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/*@ConfigurationProperties(prefix = "alipay")*/
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000118634491";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQClbw8muru+P6lAeecuMDaRGuCOSzRoXywHmToELR7bqAPKVvGYExX5cSpIghtJ3H6YUfLlszS2sNYwhN9eIlJ6vZFpFoqGApyF6CoPxkfoH5Kw1e7bHp1MWnobF8QKMzfTCiIAnWTj1PJZKWvdv07lGmpMa8sNIKZlc8J7jfpL3J2pAWkahCPmTthDjCRX85I3F6w97E3chmkGHikYQX9xIKkUd7NPrOvZBOzKM9SaoZbqPO0mUUJA46lc2tUBtAd/9r3QLxSavoQ643a9d9Rzk8+hZ8ZHW6SRMB7KzNt1rR1nlc+rZSNyql057pMT0diLSMaHQvBf3sKWh3CJFDUXAgMBAAECggEAAwYqsryLHgkv4NvZnACJntFs9WzuglHrDiCA7P6eJ8nEd/hq5irDJhIpgbXPJeR/8+zbkdNazVQiNjTXmHLsKw44EEG3OoflsJTz3+HyAo5BMjZW7W571Z95x/EBRmZ1SMhQos49v+Ruoa6eKCmNGacH9mmqBo1oPUaGcZfA0Y4pPZwDJxwKdeitmMH4V3AA2jzSNVE/8pSDS/jRAfRU8lYBfbDBRojlSYLCEGD4q3Gv7Fq57ExQLgwrAR0eiE6LEAVdlrs1dzpDpchQCrklwxAtR/YqkDzrdLrgN/xzVsVVOS0rBsAXXwTQPcA016g1lIHFpni94slMvwrieVf1QQKBgQDw4+wFNjJ2DxgNoSvFVpdH+HOqCYQeSP6oUZBYoCX0Fj12D2ypueie3r45Y4OqeaOR5VUlXqnkVNCixWwdShqGzX4h6EUBHfRMe/08XMK/hznUCj57dkGG3Ecy6UYZWEMYd7lPp1hQk+XGseaumprVjFWTt8lcSIbIfrZwGmlspwKBgQCvz4ASczxm6BDcRwKKTBQAQbO58rTX//x0GdGO6lvi0zRwMyXBgn+H3nykMy5x1vq8Wxjeo7QWC6BDbs4ZSEBQtcx1a83Ttvidc7Bqc0IDWw5DXE0UdAs5Ij96A5LefQDio0bfxCG9Q5cEP0ffX8ZYvvtI+2lC/XKTBYSaqtXSEQKBgFyhOFP0uEi/mx86l6tFldy+BDQhfqVqqkotOxVpV87Zcn7ghYdqjrHwszM8OhgmuChVCH3bPfcoSiYdRmqo4TRu9o6xUz+Pti5nC0TX4qdlp24LFmXBXocuU06XdzYtw3c4KpEfp40sohHavPrtRpbRZOLtrEeJOBMG7r6C2YFHAoGBAI40lvFViJvsRgcG4QUgOGVkv40+xgyZIIiAgcLJyf0UvSusE3QMrD8oNUVTqNOoAjPss5cSbw4N5Vnl/IryqCMgifrIcjo3m60v8JiKFfFE66qMtERBAPr7usBkt52DGv4295LzwB6o1zQ2Q1poy7To9bS4txHak5xcMEAywrUhAoGBALaJBFM9D36WHUWB2F/SXqM5slW76eWkJaI4vuMB4NSEr64OtgU3cKnUKalZJglgC4rfyQvsBwKG6xuvkxO4AVW3Yw1nN2Pn/x0RSfvRjED11f5XPlFFUqHJ8FrDuxn0MMn8pT5nbEOILDqhS1v6rCoxDGBVgdunIK5lTcGYoudI";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAm6ab3HbD1EN7uk+ZF9cjZ+RXH5Eyvyuw9daxHpMLkQwsK44IpmhN0QBiiz5FpFY4XS28ygnPxbxBMRCDl+FzeVTyXhk4UhQ9YUSQ5lg37ah2ZEaNsT0xNkCudQxkbIxEoJivOuQudQwdut5c2bkgwOVHo6MLNLbsmai5mbhrizv4z+RXg2BQIme2PmcwJugXau/ZAjWniCT7qq2mvQ64kYoW2jGnDPg+LmT5WVXZAPtG1cHhSPafLYRY8MOmjxqqBAgYWQaIJ0hpsofagYJKOq/9ksEtKAyiORa/fAw0h/QZJ6WyMvT9O1OdNE7fDwC5JOHcdwXQAi7raPyxLQ+oYwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://446583ho08.qicp.vip/alipay/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功， 跳转到列表页面  接口获取所有订单
    private  String return_url = "http://localhost:88/api/activity/shoporder/getOrderList";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\"1m\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
